#!/usr/bin/env python3
"""
基础使用示例

展示如何使用ResearchAgent生成研究报告
"""

import asyncio
import os
import sys
from pathlib import Path

# 添加src到Python路径
sys.path.insert(0, str(Path(__file__).parent / "src"))

from src.agents.research_agent import ResearchAgent
from dotenv import load_dotenv

load_dotenv()

async def basic_example():
    """基础使用示例"""
    print("🤖 基础使用示例")
    print("=" * 50)
    
    # 创建研究代理
    agent = ResearchAgent()
    
    # 示例1：生成综合报告
    print("\n📊 示例1：生成综合报告")
    topic = "人工智能在医疗中的应用"
    
    report = await agent.generate_research_report(
        topic=topic,
        report_type="comprehensive",
        max_search_results=8,
        save_to_file=True
    )
    
    print(f"✅ 报告已生成: {topic}")
    print(f"📁 文件: {report.get('file_path')}")
    print(f"📊 统计: {report['statistics']}")
    
    # 示例2：生成简要报告
    print("\n📋 示例2：生成简要报告")
    brief_topic = "区块链技术"
    
    brief_report = await agent.generate_research_report(
        topic=brief_topic,
        report_type="brief",
        max_search_results=5,
        save_to_file=True
    )
    
    print(f"✅ 简要报告已生成: {brief_topic}")
    
    # 示例3：快速摘要
    print("\n⚡ 示例3：快速摘要")
    summary = await agent.quick_search_summary("电动汽车发展趋势", max_results=3)
    
    print(f"主题: {summary['topic']}")
    print(f"摘要: {summary['summary'][:200]}...")
    print(f"来源数: {summary['sources_count']}")

async def advanced_examples():
    """高级使用示例"""
    print("\n🚀 高级使用示例")
    print("=" * 50)
    
    agent = ResearchAgent()
    
    # 示例4：自定义章节
    print("\n🎯 示例4：自定义章节报告")
    custom_topic = "可持续能源技术"
    custom_sections = [
        "环境影响评估",
        "经济效益分析",
        "政策支持情况",
        "技术成熟度比较"
    ]
    
    custom_report = await agent.generate_research_report(
        topic=custom_topic,
        report_type="comprehensive",
        custom_sections=custom_sections,
        save_to_file=True
    )
    
    print(f"✅ 自定义报告已生成: {custom_topic}")
    
    # 示例5：趋势分析
    print("\n📈 示例5：趋势分析")
    topics = [
        "人工智能",
        "机器学习",
        "深度学习",
        "自然语言处理"
    ]
    
    trend_analysis = await agent.analyze_trends(topics)
    
    print("主题趋势分析:")
    for topic, result in trend_analysis['individual_results'].items():
        if 'error' not in result:
            print(f"  - {topic}: 摘要长度 {len(result['summary'])} 字符")
    
    print(f"综合趋势分析: {trend_analysis['trend_analysis'][:300]}...")

async def configuration_demo():
    """配置演示"""
    print("\n⚙️ 配置演示")
    print("=" * 50)
    
    agent = ResearchAgent()
    
    # 显示当前配置
    print("当前配置:")
    config = agent.get_config()
    for key, value in config.items():
        print(f"  {key}: {value}")
    
    # 更新配置
    agent.update_config(
        max_search_results=15,
        report_language="zh"
    )
    
    print("\n更新后的配置:")
    new_config = agent.get_config()
    for key, value in new_config.items():
        print(f"  {key}: {value}")

async def main():
    """主函数"""
    try:
        await basic_example()
        await advanced_examples()
        await configuration_demo()
        
        print("\n🎉 所有示例执行完成！")
        print("查看 reports/ 目录获取生成的报告文件")
        
    except Exception as e:
        print(f"❌ 执行失败: {e}")

if __name__ == "__main__":
    asyncio.run(main())